package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserIndexTop
//@description: 创建UserIndexTop记录
//@param: userIndexTop model.UserIndexTop
//@return: err error

func CreateUserIndexTop(userIndexTop model.UserIndexTop) (err error) {
	err = global.GVA_DB.Create(&userIndexTop).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserIndexTop
//@description: 删除UserIndexTop记录
//@param: userIndexTop model.UserIndexTop
//@return: err error

func DeleteUserIndexTop(userIndexTop model.UserIndexTop) (err error) {
	err = global.GVA_DB.Delete(&userIndexTop).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserIndexTopByIds
//@description: 批量删除UserIndexTop记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserIndexTopByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserIndexTop{},"id in ?",ids.Ids).Error
	return err
}


func DeleteUserIndexTopByUserId(UserId int)(err error){
	err = global.GVA_DB.Delete(&[]model.UserIndexTop{},"index_id = ?",UserId).Error
	return err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserIndexTop
//@description: 更新UserIndexTop记录
//@param: userIndexTop *model.UserIndexTop
//@return: err error

func UpdateUserIndexTop(userIndexTop model.UserIndexTop) (err error) {
	err = global.GVA_DB.Save(&userIndexTop).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserIndexTop
//@description: 根据id获取UserIndexTop记录
//@param: id uint
//@return: err error, userIndexTop model.UserIndexTop

func GetUserIndexTop(id uint) (err error, userIndexTop model.UserIndexTop) {
	err = global.GVA_DB.Where("id = ?", id).First(&userIndexTop).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserIndexTopInfoList
//@description: 分页获取UserIndexTop记录
//@param: info request.UserIndexTopSearch
//@return: err error, list interface{}, total int64

func GetUserIndexTopInfoList(info request.UserIndexTopSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.UserIndexTop{})
    var userIndexTops []model.UserIndexTop
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&userIndexTops).Error
	return err, userIndexTops, total
}